package com.lepu.app.fun.my.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;

import com.app.utils.DBHelper;
import com.core.lib.utils.main.UtilityBase;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class BeanMessage {

    private static final String TABLE_NAME = "Message";

    public String MessageID;
    public String UserID;
    public String UserNickName;
    public String Avatar;
    public String ReplyType;  //回复类型
    public String ReplyContent;  //回复内容
    public String ReplyTime;
    public String QuoteReplyContent; //回复的回复 --
    public String QuoteReplyUserID; //
    public String QuoteReplyUserNickName; //
    
    public String TopicID;
    public String TopicTitle;  //主题标题
    
    public String ArticleID;   //文章id
    public String ArticleTitle;   //文章标题
    
    public String AlbumID; //热门相册
    public String AlbumCover; //热门相册
    public String AlbumName; //热门相册 名称
    public String AlbumDescription; //热门相册 描述
    
    public String IMID;   //消息id
    public String MessageTitle;   //消息标题
    public String MessageContent;   //消息内容
    public String MessageTime;   //消息时间
    public String LinkTargetType;   //连接类型
    public String LinkTargetID;   //目标id
    public String LinkURL;   //原文

    //db
    public int Id;//数据库主键Id
    public int Board;//类型   1.热门相册 2.论坛 3.文章 4.系统消息 5.好友消息
    public int IsRead;//是否已读。0-未读，1-已读
    public String LoginUserID;//用户自己的id

    public static ArrayList<BeanMessage> parseJsonData(String dataString) {
        String result = "";
        try {
            JSONObject jsonObject = new JSONObject(dataString);
            JSONObject obj1 = (JSONObject) jsonObject.get("Result");
            JSONArray dataArray = obj1.getJSONArray("ListInfo");

            result = dataArray.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Gson gson = new Gson();
        ArrayList<BeanMessage> testArraylist = gson.fromJson(result,
                new TypeToken<List<BeanMessage>>() {
                }.getType());
        return testArraylist;
    }

    public static ArrayList<BeanMessage> parseJsonSystemData(String dataString) {
    	ArrayList<BeanMessage> beanList = new ArrayList<BeanMessage>();
        try {
        	
            JSONObject jsonObject = new JSONObject(dataString);
            JSONObject obj1 = (JSONObject) jsonObject.get("Result");
            JSONArray dataArray = obj1.getJSONArray("ListInfo");
            
            for(int i=0; i<dataArray.length(); i++){
					JSONObject focusJson = (JSONObject) dataArray.get(i);
					BeanMessage bean = new BeanMessage();
					bean.IMID = focusJson.optString("PSMID");
					bean.MessageTitle = focusJson.optString("MessageTitle");
					bean.MessageContent = focusJson.optString("MessageContent");
					bean.MessageTime = focusJson.optString("MessageTime");
					bean.LinkTargetType = focusJson.optString("LinkTargetType");
					bean.LinkTargetID = focusJson.optString("LinkTargetID");
					bean.LinkURL = focusJson.optString("LinkURL");
					beanList.add(bean);
				}

        } catch (JSONException e) {
            e.printStackTrace();
        }
 
        return beanList;
    }

    //==============================================================
    public static boolean insertMyMessage(BeanMessage item) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;
        
        ContentValues values = new ContentValues();
        values.put("MessageID", item.MessageID);
        values.put("UserID", item.UserID);
        values.put("UserNickName", item.UserNickName);
        values.put("Avatar", item.Avatar);
        values.put("ReplyType", item.ReplyType);
        values.put("ReplyContent", item.ReplyContent);
        values.put("ReplyTime", item.ReplyTime);
        values.put("QuoteReplyContent", item.QuoteReplyContent);
        values.put("QuoteReplyUserID", item.QuoteReplyUserID);
        values.put("QuoteReplyUserNickName", item.QuoteReplyUserNickName);
        
        values.put("TopicID", item.TopicID);
        values.put("TopicTitle", item.TopicTitle);

        values.put("ArticleID", item.ArticleID);
        values.put("ArticleTitle", item.ArticleTitle);

        values.put("AlbumID", item.AlbumID);
        values.put("AlbumCover", item.AlbumCover);
        values.put("AlbumName", item.AlbumName);
        values.put("AlbumDescription", item.AlbumDescription);

        values.put("IMID", item.IMID);
        values.put("MessageTitle", item.MessageTitle);
        values.put("MessageContent", item.MessageContent);
        values.put("MessageTime", item.MessageTime);
        values.put("LinkTargetType", item.LinkTargetType);
        values.put("LinkTargetID", item.LinkTargetID);
        values.put("LinkURL", item.LinkURL);
        //db
        values.put("Board", item.Board);
        values.put("IsRead", item.IsRead);
        values.put("LoginUserID", item.LoginUserID);

        flag = dbHelp.insert(TABLE_NAME, null, values);

        return flag;
    }

    public static boolean deleteItem(String LoginUserID, int id) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "LoginUserID=? and Id=?",
                new String[]{LoginUserID, String.valueOf(id)});

        return flag;
    }

    public static boolean deleteAllItem(String LoginUserID, int board) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        flag = dbHelp.delete(TABLE_NAME, "LoginUserID=? and Board=?",
                new String[]{LoginUserID, String.valueOf(board)});

        return flag;
    }

    public static ArrayList<BeanMessage> getMyMessage(String LoginUserID, int board) {
    	ArrayList<BeanMessage> dataList = new ArrayList<BeanMessage>();
        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return dataList;
      
        String sql = "select * from Message where LoginUserID=? and Board=? order by ReplyTime desc";
        ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,new String[]{LoginUserID, String.valueOf(board)});

        if (queryResult == null || queryResult.size() == 0) {
            return dataList;
        }

        for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
            HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
            BeanMessage item = BeanMessage
                    .parseFromDatabase(dictionary);
            dataList.add(item);
        }
        return dataList;
    }

    public static ArrayList<BeanMessage> getMyMessage(String LoginUserID, int board, String order) {
    	ArrayList<BeanMessage> dataList = new ArrayList<BeanMessage>();
        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return dataList;
      
        String sql = "select * from Message where LoginUserID=? and Board=? order by "+order;
        ArrayList<HashMap<String, Object>> queryResult = dbHelp.query(sql,new String[]{LoginUserID, String.valueOf(board)});

        if (queryResult == null || queryResult.size() == 0) {
            return dataList;
        }

        for (int arrayIndex = 0; arrayIndex < queryResult.size(); arrayIndex++) {
            HashMap<String, Object> dictionary = queryResult.get(arrayIndex);
            BeanMessage item = BeanMessage
                    .parseFromDatabase(dictionary);
            dataList.add(item);
        }
        return dataList;
    }
    
    /*
     * 当前用户未读数据
     */
    public static int getNewMessageCount(String LoginUserID, int board) {

        int count = 0;
        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return count;
        String sql = "select count(*) from Message where LoginUserID=? and Board=? and IsRead=0";
        String result = dbHelp.executeScalar(sql, new String[]{LoginUserID, String.valueOf(board)});
        count = UtilityBase.parseInt(result);

        return count;
    }
    
    //判断消息是否存在
    public static boolean getMsgIsExist(String LoginUserID, int board, String msgId) {
    	boolean isExist = false;
    	int count = 0;
    	DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
    	if (dbHelp == null)
    		return isExist;
    	String sql = "select count(*) from Message where LoginUserID=? and Board=? and IMID=?";
    	String result = dbHelp.executeScalar(sql, new String[]{LoginUserID, String.valueOf(board), msgId});
    	count = UtilityBase.parseInt(result);
    	
    	if(count > 0){
    		isExist = true;
    	}
    	
    	return isExist;
    }

    public static boolean updateNewMessageRead(String LoginUserID, int board) {

        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        values.put("IsRead", 1);

        flag = dbHelp
                .update(TABLE_NAME, values, "LoginUserID=? and Board=?", new String[]{
                        LoginUserID, String.valueOf(board)});

        return flag;
    }


    private static BeanMessage parseFromDatabase(
            HashMap<String, Object> dictionary) {

        if (dictionary == null || dictionary.size() == 0) {
            return null;
        }
        BeanMessage item = new BeanMessage();

        item.Id = UtilityBase.parseInt((String) dictionary.get("Id"));
        item.MessageID = UtilityBase.parseString((String) dictionary.get("MessageID"));
        item.UserID = UtilityBase.parseString((String) dictionary.get("UserID"));
        item.UserNickName = UtilityBase.parseString((String) dictionary.get("UserNickName"));
        item.Avatar = UtilityBase.parseString((String) dictionary.get("Avatar"));
        item.ReplyType = UtilityBase.parseString((String) dictionary.get("ReplyType"));
        item.ReplyContent = UtilityBase.parseString((String) dictionary.get("ReplyContent"));
        item.ReplyTime = UtilityBase.parseString((String) dictionary.get("ReplyTime"));
        item.QuoteReplyContent = UtilityBase.parseString((String) dictionary.get("QuoteReplyContent"));
        item.QuoteReplyUserID = UtilityBase.parseString((String) dictionary.get("QuoteReplyUserID"));
        item.QuoteReplyUserNickName = UtilityBase.parseString((String) dictionary.get("QuoteReplyUserNickName"));

        item.TopicID = UtilityBase.parseString((String) dictionary.get("TopicID"));
        item.TopicTitle = UtilityBase.parseString((String) dictionary.get("TopicTitle"));

        item.ArticleID = UtilityBase.parseString((String) dictionary.get("ArticleID"));
        item.ArticleTitle = UtilityBase.parseString((String) dictionary.get("ArticleTitle"));

        item.AlbumID = UtilityBase.parseString((String) dictionary.get("AlbumID"));
        item.AlbumCover = UtilityBase.parseString((String) dictionary.get("AlbumCover"));
        item.AlbumName = UtilityBase.parseString((String) dictionary.get("AlbumName"));
        item.AlbumDescription = UtilityBase.parseString((String) dictionary.get("AlbumDescription"));

        item.IMID = UtilityBase.parseString((String) dictionary.get("IMID"));
        item.MessageTitle = UtilityBase.parseString((String) dictionary.get("MessageTitle"));
        item.MessageContent = UtilityBase.parseString((String) dictionary.get("MessageContent"));
        item.MessageTime = UtilityBase.parseString((String) dictionary.get("MessageTime"));
        item.LinkTargetType = UtilityBase.parseString((String) dictionary.get("LinkTargetType"));
        item.LinkTargetID = UtilityBase.parseString((String) dictionary.get("LinkTargetID"));
        item.LinkURL = UtilityBase.parseString((String) dictionary.get("LinkURL"));
        
        item.Board = UtilityBase.parseInt((String) dictionary.get("Board"));
        item.IsRead = UtilityBase.parseInt((String) dictionary.get("IsRead"));
        item.LoginUserID = UtilityBase.parseString((String) dictionary.get("LoginUserID"));

        return item;
    }
}
